package www.baidu.ofo_bike.user.controller;



import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import www.baidu.ofo_bike.common.constans.Constans;
import www.baidu.ofo_bike.common.exception.MaMaBikeException;
import www.baidu.ofo_bike.common.resp.APIResult;

import www.baidu.ofo_bike.user.entity.LoginInfo;

import www.baidu.ofo_bike.user.service.UserService;
@Slf4j
@RestController
@RequestMapping("user")
public class UserController {





    @Autowired
    @Qualifier("userServiceImpl")
    private UserService userService;

   @RequestMapping(value = "/login",method = RequestMethod.POST,produces =MediaType.APPLICATION_JSON_VALUE)
    public APIResult login(@RequestBody LoginInfo loginInfo){
       APIResult<String> result=new APIResult<>();
       try {
           String data=loginInfo.getData();
           String key=loginInfo.getKey();
           if (StringUtils.isBlank(data)||StringUtils.isBlank(key)){
               throw  new MaMaBikeException("参数校验失败");
           }
           String token=userService.login(data,key);
           result.setData(token);


       } catch (MaMaBikeException e){
           result.setMessage(e.getMessage());
           result.setCoder(Constans.RESP_STATUS_INTERNAL_ERROR);

       } catch (Exception e) {
            log.error("Fail to login",e);
            result.setCoder(Constans.RESP_STATUS_INTERNAL_ERROR);
            result.setMessage("内部错误");
       }
       return  result;
    }
}
